package com.opensource.component.extend.point.metadata;

import org.springframework.beans.factory.config.BeanDefinition;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanglong and since on 2022/12/19 8:39 下午
 */
public class SpecificExtendMetadataHelper {

    private static final List<SpecificExtendMetadata> extend = new ArrayList<>();


    public static List<SpecificExtendMetadata> extendPointMetadata() {
        return extend;
    }

//    public static List<String> supperBeanDefinitionNames() {
//        return extend.stream().map(SpecificExtendMetadata::getSupperBeanDefinitionName).collect(Collectors.toList());
//    }


//    public static void populateSuperBeanClass(Class<?> supperClazz, BeanDefinition candidateComponent) {
//        final List<SpecificExtendMetadata> extendList = extend.stream().filter(m -> Objects.nonNull(m.getExtendClass())).filter(m -> supperClazz.isAssignableFrom(m.getExtendClass())).collect(Collectors.toList());
//        if (extendList.size() > 1) {
//            throw new RuntimeException("Bean Class [" + supperClazz.getName() + "] has more @SpecificExtend Impl ["
//                    + extendList.stream().map(SpecificExtendMetadata::getExtendClass).map(Class::getName).collect(Collectors.joining(",")) + "]");
//        } else if (extendList.size() == 1) {
//            final SpecificExtendMetadata extendPointMetadata = extendList.get(0);
//            extendPointMetadata.setSupperBeanClass(supperClazz);
//            extendPointMetadata.setSupperBeanDefinitionName(candidateComponent.getBeanClassName());
//        }
//    }

    public static void populateExtendPoint(Class<?> extendClass, Class<?> superBeanClass, BeanDefinition candidateComponent) {
        final SpecificExtendMetadata specificExtendMetadata = new SpecificExtendMetadata();
        specificExtendMetadata.setExtendClass(extendClass);
        specificExtendMetadata.setSuperBeanClass(superBeanClass);
        specificExtendMetadata.setExtendBeanDefinition(candidateComponent);
        extend.add(specificExtendMetadata);
    }
}
